package com.example.demo.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Configuration
@EnableOpenApi
public class SwaggerConfig {

    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("标准接口")
                .apiInfo(apiInfo("springboot中使用swagger2构建RESTful API","1.0"))
                .useDefaultResponseMessages(true)
                .forCodeGeneration(false)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) // 替换为你的controller包路径
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo(String title,String version) {
        return new ApiInfoBuilder()
                .title(title)
                .description("接口文档描述")
                .version(version)
                .contact(new Contact("联系人名字", "http://xxx.xxx.com/联系人访问链接", "联系人邮箱"))
                .build();
    }

//    /**
//     * 解决swagger在springboot2.7以后的空指针异常
//     */
//    @Bean
//    public static BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
//
//        return new BeanPostProcessor() {
//
//            @Override
//            public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
//
//                if (bean instanceof WebMvcRequestHandlerProvider) {
//
//                    customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
//
//                }
//                return bean;
//            }
//
//
//            private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
//
//                List<T> copy = mappings.stream()
//
//                        .filter(mapping -> mapping.getPatternParser() == null)
//
//                        .collect(Collectors.toList());
//
//                mappings.clear();
//
//                mappings.addAll(copy);
//
//            }
//
//
//            @SuppressWarnings("unchecked")
//            private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
//                try {
//
//                    Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
//                    if (Objects.nonNull(field)) {
//                        field.setAccessible(true);
//                        return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
//                    }
//                    return Collections.emptyList();
//                } catch (IllegalArgumentException | IllegalAccessException e) {
//                    throw new IllegalStateException(e);
//                }
//            }
//
//        };
//
//    }



}
